package com.ld.translate;

import com.alibaba.fastjson.JSONObject;
import com.ld.utils.CommonUtils;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Optional;

public class TranslateFactory implements Translate{

    private static String configFileName = "proble.properties";

    private PropertiesConfiguration properties = null;

    private static final TranslateFactory kit = new TranslateFactory();

    public TranslateFactory(){
        String userHome = System.getProperty("user.home");
        File file = Paths.get(userHome, "translation").toFile();
        if (!file.exists()){
            boolean mkdir = file.mkdir();
            System.out.println("创建目录:"+mkdir);
        }
        if (file.exists()){
            File toFile = Paths.get(file.getAbsolutePath(), configFileName).toFile();
            if (!toFile.exists()){
                try {
                    boolean newFile = toFile.createNewFile();
                    System.out.println("创建文件:"+newFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (toFile.exists()){
                try {
                    properties = new PropertiesConfiguration(toFile.getAbsoluteFile());
                } catch (ConfigurationException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static TranslateFactory getInstance(){
        return kit;
    }

    public Translate getTranslate(){
        return new AliTranslate();
    }



    public String tokens(String word){
        return new Analysis().tokens(word);
    }

    public void putCache(String key,String value){
        if (properties != null){
            properties.setProperty(key, value);
        }
    }

    public Optional<String> getCache(String key){
        if (properties == null){
            return Optional.empty();
        }
        String string = properties.getString(key);
        if (StringUtils.isBlank(string)){
            return Optional.empty();
        }
        return Optional.ofNullable(string);
    }

    public PropertiesConfiguration getProperties() {
        return properties;
    }

    @Override
    public JSONObject translate(String sourceText, String sourceLanguage, String targetLanguage) throws Exception {
        Optional<String> cache = getCache(sourceText);
        if (cache.isPresent()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",200);
            jsonObject.put("data",cache.get());
            return jsonObject;
        }else {
            Translate translate = getTranslate();
            try {
                JSONObject jsonObject = translate.translate(sourceText, sourceLanguage, targetLanguage);
                if (jsonObject.getInteger("code") == 200){
                    putCache(sourceText,jsonObject.getString("data"));
                }
                return jsonObject;
            }catch (Exception e){
                e.printStackTrace();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code",500);
                return jsonObject;
            }
        }
    }
}

class Analysis{
    public String tokens(String word){
        StringBuilder sb = new StringBuilder();
        if (isConstant(word)){
            String[] split = word.split("_");
            for (int i = 0; i < split.length; i++){
                sb.append(split[i].toLowerCase());
                if (i+1 != split.length){
                    sb.append(" ");
                }
            }
        }else {
            int length = word.length();
            for (int i = 0; i < length; i++){
                String s = String.valueOf(word.charAt(i));
                if (CommonUtils.isMatch("[A-Z]",s)){
                    sb.append(" ");
                }else if ("_".equals(s)){
                    s = "";
                }
                sb.append(s);
            }
        }
        return sb.toString();
    }

    public boolean isConstant(String word){
        String[] split = word.split("_");
        boolean flag = true;
        for (int i = 0; i < split.length;i++){
            if (!CommonUtils.isMatch("[A-Z]",split[i])){
                flag = false;
                break;
            }
        }
        return flag;
    }

}
